package com.xianbeiying.app.ckgl.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;
import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.ChoiceDeatilActivity;
import com.xianbeiying.app.ckgl.activity.ExpertSystemActivity;
import com.xianbeiying.app.ckgl.activity.HistoryDataActivityss;
import com.xianbeiying.app.ckgl.activity.LoginActivity;
import com.xianbeiying.app.ckgl.activity.ModePlantActivity;
import com.xianbeiying.app.ckgl.activity.NetdoLibssActivity;
import com.xianbeiying.app.ckgl.activity.RealTimeModeActivity;
import com.xianbeiying.app.ckgl.activity.RealTimeMonitorActivity;
import com.xianbeiying.app.ckgl.activity.RealTimessInfoActivity;
import com.xianbeiying.app.ckgl.activity.SystemConfigActivity;
import com.xianbeiying.app.ckgl.activity.VaorRbActivity;
import com.xianbeiying.app.ckgl.activity.WateringPlanssActivity;
import com.xianbeiying.app.ckgl.activity.WeatherActivity;
import com.xianbeiying.app.ckgl.adapter.HomeListdapter;
import com.xianbeiying.app.ckgl.bean.DpWatherBean;
import com.xianbeiying.app.ckgl.bean.JINGXUNBean;
import com.xianbeiying.app.ckgl.bean.NoticesBean;
import com.xianbeiying.app.ckgl.bean.WeatherBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.model.WeatherModel;
import com.xianbeiying.app.ckgl.util.CityCallBack;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.widght.NoScrollListView;
import com.xianbeiying.app.ckgl.widght.VerticalTextview;

import java.util.ArrayList;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener, HttpCallBackUtil, CityCallBack {
    private View view;
    private ImageView iv_title_left;
    private TextView tv_title;
    private static HomeFragment homeFragment;
    private ScrollView sc_home;

    public static HomeFragment getInstance() {
        homeFragment = new HomeFragment();
        return homeFragment;
    }

    private WeatherBean weatherBean;
    private DpWatherBean dpWatherBean;
    private WeatherModel weatherModel;
    private GetModel getModel;
    //公告
    TextView tv_home_gg;
    VerticalTextview tv_va_choose;
    //灌溉技术
    RelativeLayout rl_gg;
    //风机卷帘
    RelativeLayout rl_fj;
    //加热器
    RelativeLayout rl_szd;
    //实时监控
    RelativeLayout rl_ssjk;
    //模型种植
    RelativeLayout rl_mxzz;
    //历史数据
    RelativeLayout rl_lssj;
    //实时信息
    RelativeLayout rl_ssxx;
    //系统配置
    LinearLayout ly_xtpz;
    //实时状态
    LinearLayout rl_sszt;
    //专家系统
    RelativeLayout rl_zhuanjia;
    NoScrollListView home_list;
    //天气
    ImageView iv_weather;
    TextView tv_home_date;
    TextView tv_sheshidu;
    TextView tv_weather_zt;
    TextView tv_pj, tv_zd, tv_zg;
    private RelativeLayout rl_weather;
    //判断标题是否有数据
    private boolean isTitle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        initView();
        return view;
    }

    private void initView() {
        sc_home = (ScrollView) view.findViewById(R.id.sc_home);
        rl_weather = (RelativeLayout) view.findViewById(R.id.rl_weather);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        iv_title_left = (ImageView) view.findViewById(R.id.iv_title_left);
        iv_weather = (ImageView) view.findViewById(R.id.iv_weather);
        tv_home_gg = (TextView) view.findViewById(R.id.tv_home_gg);
        tv_home_date = (TextView) view.findViewById(R.id.tv_home_date);
        tv_sheshidu = (TextView) view.findViewById(R.id.tv_sheshidu);
        tv_va_choose = (VerticalTextview) view.findViewById(R.id.tv_va_choose);
        tv_weather_zt = (TextView) view.findViewById(R.id.tv_weather_zt);
        tv_pj = (TextView) view.findViewById(R.id.tv_pj);
        tv_zd = (TextView) view.findViewById(R.id.tv_zd);
        tv_zg = (TextView) view.findViewById(R.id.tv_zg);
        iv_title_left.setVisibility(View.GONE);
        rl_gg = (RelativeLayout) view.findViewById(R.id.rl_gg);
        rl_fj = (RelativeLayout) view.findViewById(R.id.rl_fj);
        rl_szd = (RelativeLayout) view.findViewById(R.id.rl_szd);
        rl_ssjk = (RelativeLayout) view.findViewById(R.id.rl_ssjk);
        rl_mxzz = (RelativeLayout) view.findViewById(R.id.rl_mxzz);
        rl_lssj = (RelativeLayout) view.findViewById(R.id.rl_lssj);
        rl_ssxx = (RelativeLayout) view.findViewById(R.id.rl_ssxx);
        ly_xtpz = (LinearLayout) view.findViewById(R.id.ly_xtpz);
        rl_sszt = (LinearLayout) view.findViewById(R.id.rl_sszt);
        rl_zhuanjia = (RelativeLayout) view.findViewById(R.id.rl_zhuanjia);
        home_list = (NoScrollListView) view.findViewById(R.id.home_list);
        tv_title.setText("首页");
        sc_home.smoothScrollBy(0, 0);
        initData();
        initEvent();
    }

    private String city;
    private ArrayList<String> titleList = new ArrayList<String>();

    private void initData(){
        weatherModel = new WeatherModel(getActivity());
        weatherModel.addResponseListener(this);
        getModel = new GetModel(getActivity());
        getModel.addResponseListener(this);
        if (MyApplication.devicesBean!= null) {
            city = MyApplication.devicesBean.getGREENHOUSE().get(0).getGreenhouseAddress();
            if (MyApplication.dpWatherBean != null) {
                dpWatherBean = MyApplication.dpWatherBean;
                showDpata();
            } else {
                weatherModel.getWeather(city, true);
            }
        } else {
//            //设置默认
//            city = "西安";
//            weatherModel.getWeather(city, true);
//        if (MyApplication.weatherBean != null){
//            weatherBean = MyApplication.weatherBean;
//            showData();
//        }else{
//            if (MyApplication.myMapLocation != null){
//                weatherModel.getWeather(MyApplication.myMapLocation.getCity(), true);
//                weatherModel.getWeatherData(MyApplication.myMapLocation.getCity(), true);
//            } else {
//                MyApplication.locationClient.startLocation();
//                new RegUtil().getCallBack(this);
//            }
        }
//    }

        if (MyApplication.noticesBean != null) {
            noticesBean = MyApplication.noticesBean;
            setNoticeTitle();
        } else {
            getModel.GETNotice("list", true);
        }
        if (MyApplication.jingxunBean != null) {
            jingxunBean = MyApplication.jingxunBean;
            setJingXuan();
        } else {
            getModel.JINGXUAN("choice", true);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isTitle)
            tv_va_choose.startAutoScroll();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isTitle)
            tv_va_choose.stopAutoScroll();
    }

    private void initEvent() {
        rl_gg.setOnClickListener(this);
        rl_fj.setOnClickListener(this);
        rl_szd.setOnClickListener(this);
        rl_ssjk.setOnClickListener(this);
        rl_mxzz.setOnClickListener(this);
        rl_lssj.setOnClickListener(this);
        rl_ssxx.setOnClickListener(this);
        ly_xtpz.setOnClickListener(this);
        rl_sszt.setOnClickListener(this);
        rl_zhuanjia.setOnClickListener(this);
        iv_title_left.setOnClickListener(this);
        rl_weather.setOnClickListener(this);
        tv_home_gg.setOnClickListener(this);
        home_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent().setClass(getActivity(), ChoiceDeatilActivity.class).putExtra("jingxunBean", jingxunBean.getINFOS().get(position)));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            //公告
            case R.id.tv_home_gg:
//                startActivity(new Intent().setClass(getActivity(), CircleActivity.class));
                break;
            case R.id.rl_weather:
                if (dpWatherBean != null) {
                    startActivity(new Intent().setClass(getActivity(), WeatherActivity.class).putExtra("dpWatherBean", dpWatherBean));
                }
                break;
            case R.id.iv_title_left:
                getActivity().finish();
                break;
            //灌溉技术
            case R.id.rl_gg:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), WateringPlanssActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //风机卷帘
            case R.id.rl_fj:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), VaorRbActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //加热器
            case R.id.rl_szd:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), NetdoLibssActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //实时监控
            case R.id.rl_ssjk:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), RealTimeMonitorActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //模型种植
            case R.id.rl_mxzz:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), ModePlantActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //历史数据
            case R.id.rl_lssj:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), HistoryDataActivityss.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //实时信息
            case R.id.rl_ssxx:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), RealTimessInfoActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //系统配置
            case R.id.ly_xtpz:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), SystemConfigActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //实时状态
            case R.id.rl_sszt:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), RealTimeModeActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
            //专家系统
            case R.id.rl_zhuanjia:
                if (MyApplication.isLogin) {
                    startActivity(new Intent().setClass(getActivity(), ExpertSystemActivity.class));
                } else {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                }
                break;
        }
    }

    @Override
    public void onCityBackData(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            MyApplication.myMapLocation = aMapLocation;
            MyApplication.locationClient.startLocation();
//          weatherModel.getWeatherData(aMapLocation.getCity(), false);
            weatherModel.getWeather(aMapLocation.getCity(), true);
        }
    }

    private NoticesBean noticesBean;
    private JINGXUNBean jingxunBean;

    @Override
    public void onCallBackData(String url, Object content) {
//        if (url.equals("天气")) {
////            DialogUtile.closeDialog();
//            weatherBean = (WeatherBean) content;
//            MyApplication.weatherBean = weatherBean;
//            showData();
//        }
        if (url.equals("大棚天气")) {
            dpWatherBean = JSON.parseObject(new String((byte[]) content), DpWatherBean.class);
//            if (!city.equals("西安")) {
            MyApplication.dpWatherBean = dpWatherBean;
//            }
            showDpata();
        }

        if (url.equals("公告信息")) {
            if (content != null) {
                noticesBean = JSON.parseObject(new String((byte[]) content), NoticesBean.class);
                LogUtils.e("----");
                MyApplication.noticesBean = noticesBean;
                setNoticeTitle();
                LogUtils.e("=====");
            }
        }
//        if (url.equals("首页精选")) {
//            if (content != null) {
//                jingxunBean = JSON.parseObject(new String((byte[]) content), JINGXUNBean.class);
//                MyApplication.jingxunBean = jingxunBean;
//                setJingXuan();
//
//            }
//        }
    }

    private void setJingXuan() {
        home_list.setAdapter(new HomeListdapter(getActivity(), jingxunBean));
    }

    private void setNoticeTitle() {
        titleList.clear();
        if (noticesBean.getNOTICE()!= null && noticesBean.getNOTICE().size() != 0) {
            int size = noticesBean.getNOTICE().size();
            for (int i = 0; i < size; i++) {
                isTitle = true;
                titleList.add(noticesBean.getNOTICE().get(i).getTitle());
            }
            if (isTitle) {
                tv_va_choose.setTextList(titleList);
                if(getActivity()!=null)
                tv_va_choose.setText(17, 0, ContextCompat.getColor(getActivity(), R.color.notice));//设置属性
                tv_va_choose.setTextStillTime(3000);//设置停留时长间隔
                tv_va_choose.setAnimTime(300);//设置进入和退出的时间间隔
                tv_va_choose.startAutoScroll();
            }
        }
    }

//    private void showData() {
//        tv_home_date.setText(DataUtil.getRQ(weatherBean.getDate()) + "\n" + weatherBean.getWeek());
//        String wendu[] = StringUtil.address(weatherBean.getTemperature(), "~");
//        int avg = (Integer.valueOf(StringUtil.sprintlaststring(wendu[0], "℃")) + Integer.valueOf(StringUtil.sprintlaststring(wendu[1], "℃"))) / 2;
//        tv_sheshidu.setText(avg + "℃");
//        tv_weather_zt.setText(weatherBean.getWeather() + "\n" + weatherBean.getWind());
//        tv_pj.setText(avg + "℃");
//        tv_zd.setText(wendu[0]);
//        tv_zg.setText(wendu[1]);
//        weatherModel.setImage(weatherBean.getWeather(), iv_weather);
//    }

    private void showDpata() {
        tv_home_date.setText(DataUtil.getRQs(dpWatherBean.getData().getForecast().get(0).getDate(),0));
        int max = Integer.valueOf(DataUtil.getTemp(dpWatherBean.getData().getForecast().get(0).getHigh(), "高温", "℃"));
        int min = Integer.valueOf(DataUtil.getTemp(dpWatherBean.getData().getForecast().get(0).getLow(), "低温", "℃"));
        int avg = (max + min) / 2;
        tv_sheshidu.setText(dpWatherBean.getData().getWendu() + "℃");
        tv_weather_zt.setText(dpWatherBean.getData().getForecast().get(0).getType() + "\n" + dpWatherBean.getData().getForecast().get(0).getFengli());
        tv_pj.setText(avg + "℃");
        tv_zd.setText(min + "℃");
        tv_zg.setText(max + "℃");
        weatherModel.setImage(dpWatherBean.getData().getForecast().get(0).getType(), iv_weather);
    }
}
